﻿using Core.Factory;
using EntityFramework;
using Model;
using NLog;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Commons;

namespace Core.Service
{
    public class UserService : IUserService
    {
        private readonly Logger Logger = LogManager.GetCurrentClassLogger();

        private readonly MyDbContext _myDbContext;
        public UserService(MyDbContext myDbContext)
        {
            _myDbContext = myDbContext;
            //Logger.Debug("实例化BaseService");
        }

        public void AddPoints(T_PointsDetail detail)
        {
            var rule = _myDbContext.T_PointsRule.FirstOrDefault(i => i.Code == detail.PointsRule);

            //是否继续累计积分
            int count = _myDbContext.T_PointsDetail.Where(i => i.UserId == detail.UserId && rule.Code == i.PointsCode && i.CreateDate.Date == DateTime.Now.Date).Count();
            if (count == rule.Limit)
            {
                throw new Exception("今日已达上线！");
            }
            detail.Points = rule.Points;
            detail.PointsRule = rule.RuleTitle;
            detail.PointsCode = rule.Code;
            detail.CreateDate = DateTime.Now;
            _myDbContext.T_PointsDetail.Add(detail);
            var user = _myDbContext.T_User.First(i => i.Id == detail.UserId);
            user.Points += detail.Points;
            _myDbContext.SaveChanges();
        }

        public T_User LoginValidate(T_User user)
        {
            var model = _myDbContext.T_User.FirstOrDefault(i => i.WxOpenId == user.WxOpenId);
            if (model == null)
            {
                user.CreateDate = DateTime.Now;
                user.LotteryNum = 0;
                user.Points = 0;
                user.StateCode = StateCode.Enable.ToString();
                model = _myDbContext.T_User.Add(user).Entity;
                _myDbContext.SaveChanges();
            }
            else
            {
                model.WxPhoto = user.WxPhoto;
                model.WxNumber = user.WxNumber;
                model.WxName = user.WxName;
                model.Sex = user.Sex;
                model.Phone = user.Phone;
                model.Name = user.Name;
                model.Email = user.Email;
                model.WxUserId = user.WxUserId;
            }
            return model;

        }

        public T_BuyRecord NewBuy(T_BuyRecord t_Buy)
        {
            var buy = _myDbContext.T_BuyRecord.Add(t_Buy).Entity;
            _myDbContext.SaveChanges();
            return buy;
        }

        public void UpdateBuy(int id, int userId, bool isCancel)
        {
            var buy = _myDbContext.T_BuyRecord.SingleOrDefault(i => i.Id == id && i.UserId == userId);
            buy.State = isCancel ? "已取消" : "已支付";
            if (!isCancel)
            {
                var user = _myDbContext.T_User.SingleOrDefault(i => i.Id == userId);
                var vip = _myDbContext.T_VipModel.SingleOrDefault(i => i.Code == buy.VipModel);
                user.ExpireDate = DateTime.Now.AddDays(vip.Value);
            }
            _myDbContext.SaveChanges();
        }
    }
}
